package com.office.sysManage.staffManage.controller;


import com.office.sysManage.deptManage.pojo.Dept;
import com.office.sysManage.deptManage.service.DeptService;
import com.office.sysManage.roleManage.service.RoleService;
import com.office.sysManage.staffManage.pojo.Position;
import com.office.sysManage.staffManage.pojo.Staff;
import com.office.sysManage.staffManage.service.StaffService;
import com.office.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 88168
 * @since 2022-10-24
 */
@RestController
@RequestMapping("/staff")
public class StaffController {

    @Autowired
    private StaffService staffService;

    public StaffService getStaffService() {
        return staffService;
    }
    public void setStaffService(StaffService staffService) {
        this.staffService = staffService;
    }

    @Autowired
    private DeptService deptService;

    public DeptService getDeptService() {
        return deptService;
    }
    public void setDeptService(DeptService deptService) {
        this.deptService = deptService;
    }

    @Autowired
    private RoleService roleService;

    public RoleService getRoleService() {
        return roleService;
    }
    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    /**
     * 根据条件查询员工信息
     * @param staff 员工对象
     * @return 员工列表
     */
    @ResponseBody
    @RequestMapping("/queryStaffByCondition")
    public PageModel<Staff> queryStaffByCondition(Staff staff,PageModel<Staff> pageModel) {
        return staffService.queryStaffByCondition(staff,pageModel);
    }

    /**
     * 根据条件查询员工信息
     * @param staff 员工对象
     * @return 员工
     */

    @RequestMapping("/queryStaffInfo")
    public Staff queryStaffInfo(@RequestBody Staff staff) {
        System.out.println(staff);
        return staffService.queryStaffByCondition(staff);
    }

    /**
     * 添加员工信息
     * @param staff 员工对象
     * @return 是否添加成功
     */
    @RequestMapping("/addStaff")
    public boolean addStaff(@RequestBody Staff staff) {
        if (staff != null){
            Dept dept = new Dept();
            dept.setDeptName(staff.getDeptName());
            int newDeptId = deptService.queryDeptIdByName(dept);
            staff.setDeptId(newDeptId);
            return staffService.addStaff(staff);
        }
        return false;
    }

    /**
     * 逻辑删除员工信息
     * @param staff 员工对象
     * @return 是否删除成功
     */
    @RequestMapping("/deleteStaff")
    public boolean deleteStaff(Staff staff) {
        return staffService.deleteStaff(staff);
    }

    /**
     * 获取员工信息详情
     * @param staff
     * @return
     */
    @RequestMapping("/queryStaffByStaffId")
    public Staff queryStaffByStaffId(@RequestBody Staff staff,PageModel<Staff> pageModel){
        if (staffService.queryStaffByCondition(staff,pageModel).getData().size()>0){
            return staffService.queryStaffByCondition(staff,pageModel).getData().get(0);
        }
        return null;
    }

    /**
     * 查询所有的职位
     * @return 职位列表
     */
    @RequestMapping("/queryPositionList")
    public List<Position> queryPositionList() {
        return staffService.queryPositionList();
    }

    @RequestMapping("/updateStaff")
    public boolean updateStaff(@RequestBody Staff staff){
        if (staff != null){
            // 设置角色ID
            int roleId = roleService.queryRoleIdByName(staff.getRoleName());
            staff.setRoleId(roleId);
            // 设置部门ID
            Dept dept = new Dept();
            dept.setDeptName(staff.getDeptName());
            int deptId = deptService.queryDeptIdByName(dept);
            staff.setDeptId(deptId);
            return staffService.updateStaff(staff);
        }
        return false;
    }

}

